package com.attendance.controller.api;

import com.alibaba.fastjson2.JSON;
import com.attendance.entity.Flag;
import com.attendance.entity.FlagClockRecord;
import com.attendance.service.impl.FlagServiceImpl;
import com.attendance.utils.ResponseInfo;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/api/flag")
public class FlagController {

    @Resource
    FlagServiceImpl service;

    @Value("${file.upload-dir}")
    private String uploadLocation;

    @GetMapping("/get")
    public String getFlagData(@Param("email") String email) {
        List<Flag> flags = service.selectFlagByEmail(email);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("email", email);
        map.put("flags", flags);
        return JSON.toJSONString(ResponseInfo.success(map, "获取成功 ✅"));
    }

    @GetMapping("/clock")
    public String getClockByEmailAndID(@Param("email") String email, @Param("flag_id") String flag_id) {
        List<FlagClockRecord> flagsc = service.selectFlagByEmailAndID(email, Integer.valueOf(flag_id));
        System.out.println(JSON.toJSONString(flagsc));
        HashMap<Object, Object> map = new HashMap<>();
        map.put("flagClockRecord", flagsc);
        map.put("email", email);
        return JSON.toJSONString(ResponseInfo.success(map));
    }

    @PostMapping("checktime")
    public String checkTime(@RequestBody FlagClockRecord fcrd){
        HashMap<Object, Object> map = new HashMap<>();
        service.clockingFlag(fcrd);
        return JSON.toJSONString(ResponseInfo.success(map, "打卡成功 ✅"));
    }

    @PostMapping("/upload")
    public ResponseEntity<String> uploadFile(@RequestParam("clockfile") MultipartFile file) {
        if (file.isEmpty()) {
            return ResponseEntity.badRequest().body("文件为空");
        }
        try {
            String fileName = UUID.randomUUID().toString() + "_" + file.getOriginalFilename();
            Path filePath = Paths.get(uploadLocation, fileName);
            Files.createDirectories(filePath.getParent());
            Files.write(filePath, file.getBytes());
            String fileUrl = "/static/" + fileName;
            return ResponseEntity.ok(fileUrl);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.internalServerError().body("文件上传失败");
        }
    }

    @PostMapping("/add")
    public String addFlag(@RequestBody Flag flag) {
        HashMap<Object, Object> map = new HashMap<>();
        List<Flag> flags = service.selectFlagOverByEmail(flag.getEmail());
        if (flags.size() >= 3) {
            return JSON.toJSONString(ResponseInfo.fail("最多同时存在三个Flag", 500));
        } else {
            service.insert(flag);
            return JSON.toJSONString(ResponseInfo.success(map, "创建成功 ✅"));
        }
    }

}
